<?php


namespace app\admin\model;

use think\Db;
use think\Model;

// 检车项目管理

class Project extends Model
{
    public function getList($pageNum, $numPerPage,$license_type)
    {
        $map = ['is_delete' => 0];
        if ($license_type) {
            $map['license_type'] = ['like', "%{$license_type}%"];
        }
        //$list = db('vehicle_inspection')
        //    ->field("goods_id,license_type,create_time,update_time,goods_name,shop_price,sort")
        //    ->where($map)
        //    ->page($pageNum, $numPerPage)
        //    ->order('sort, create_time desc')
        //    ->select();
        $list = Db::view('cd_vehicle', ['title' => 'vehicle_type'])
            ->view('cd_vehicle_inspection', 'goods_id,license_type,create_time,update_time,goods_name,shop_price,sort', 'cd_vehicle.id=cd_vehicle_inspection.license_type')
            ->where($map)
            ->page($pageNum, $numPerPage)
            ->order('sort, create_time desc')
            ->select();
        return $list;
    }
    // 获取总数
    public function getCount($license_type)
    {
        $map = ['is_delete' => 0];
        if ($license_type) {
            $map['license_type'] = ['like', "%{$license_type}%"];
        }
        $count = db('vehicle_inspection')
            ->field("goods_id")
            ->where($map)
            ->count();
        return $count;
    }
    // 执行添加
    public function doAdd_project($data)
    {
        if(empty($data['inspection_id'])){
            exception('检车项目，至少选择一项');
        }
        $data['goods_name'] = db('inspection')->where('id', '=', $data['inspection_id'])->value('title');
        $data['is_delete'] = 0;
        $one = db('vehicle_inspection')
            ->where('inspection_id',$data['inspection_id'])
            ->where('license_type',$data['license_type'])
            ->where('is_delete',$data['is_delete'])
            ->find();
        if(empty($data['is_company'])){
            $data['create_time'] = time();
            $data['update_time'] = time();
            if($one){
                exception('检车项目已存在，请重新输入');
            }
            $result = db('vehicle_inspection')
                ->insert($data);
        }else{
            if(empty($one)){
                exception('管理员未设置该检车项目，请重新选择');
            }
            unset($data['is_company'], $data['inspection_id'], $data['is_delete']);
            $two = db('vehicle_inspection_company')
                ->where('inspection_id',$one['goods_id'])
                ->where('license_type',$data['license_type'])
                ->where('company_id',$data['company_id'])
                ->find();
            if($two){
                exception('检车项目已存在，请重新输入');
            }
            $data['inspection_id'] = $one['goods_id'];
            $result = db('vehicle_inspection_company')
                ->insert($data);
        }
        if (!$result) {
            exception('添加失败');
        }
    }
    //执行删除
    public function doDelete($userId, $is_company)
    {
        if(0 == $is_company){
            $result = db('vehicle_inspection')
                ->where('goods_id', $userId)
                ->delete();
        }else{
            $result = db('vehicle_inspection_company')
                ->where('id', $userId)
                ->delete();
        }
        if (!$result) {
            exception('删除失败');
        }
    }
    // 执行修改
    public function doModify_project($data)
    {
        $id = $data['id'];
        $is_company = $data['is_company'];
        unset($data['id'], $data['is_company'], $data['inspection_id'], $data['license_type']);
        if(empty($is_company)){
            $data['update_time'] = time();
            $result = db('vehicle_inspection')
                ->where('goods_id',$id)
                ->update($data);
        }else{
            $result = db('vehicle_inspection_company')
                ->where('id',$id)
                ->update($data);
        }
        if (!$result) {
            exception('修改失败');
        }
    }
    // 获取分站检车项目列表
    public function getCompanyList($pageNum, $numPerPage, $company_id)
    {
        $map = ['is_delete' => 0, 'cd_company.is_use' => 1];
        if ($company_id) {
            $map['company_id'] = $company_id;
        }
        $list = Db::view('cd_vehicle_inspection_company')
                  ->view('cd_vehicle', ['title' => 'vehicle_type'], 'cd_vehicle.id=cd_vehicle_inspection_company.license_type')
                  ->view('cd_company', 'company_name', 'cd_company.id=cd_vehicle_inspection_company.company_id')
                  ->where($map)
                  ->page($pageNum, $numPerPage)
                  ->order('cd_vehicle_inspection_company.id')
                  ->select();
        return $list;
    }


    // 获取分站检车项目总数
    public function getCompanyCount($company_id)
    {
        $map = [];
        if ($company_id) {
            $map['company_id'] = $company_id;
        }
        $count = db('vehicle_inspection_company')
            ->where($map)
            ->count();
        return $count;
    }

    /**
     * 状态-启用&禁用
     */
    public function editStatus($data)
    {
        $result = db('vehicle_inspection_company')->update($data);
        if(!$result){
            exception("操作失败");
        }
        return $result;
    }
}